package com.yimi.policyservice.repository;

import com.yimi.policyservice.entity.base.system.SysUserRole;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface SysUserRoleRepsitory extends JpaRepository<SysUserRole,String> {

    /**
     * 删除用户角色
     * @param ids
     */
    @Transactional
    @Modifying
    @Query(value = "delete from t_sys_user_role where user_id in ?1 ",nativeQuery=true)
    void deleteByUserIdIn(List<String> ids);

    /**
     * 获取用户角色id
     * @param id
     * @return
     */
    @Query(value = "select role_id from t_sys_user_role where user_id = ?1  ",nativeQuery=true)
    List<String> findRoleIdByUserId(String id);


    /**
     * 删除角色下的用户角色
     * @param id
     */
    @Transactional
    @Modifying
    @Query(value = "delete from t_sys_user_role where role_id = ?1",nativeQuery=true)
    void deleteByRoleId(String id);

    @Query(value = "select r.name from t_sys_role r where r.id in (select role_id from t_sys_user_role where user_id = ?1)" , nativeQuery = true)
    List<String> getUserRoleNames(String id);

    @Query(value = "select r.id from t_sys_role r where permissions = ?2 and r.id in (select role_id from t_sys_user_role where user_id = ?1) LIMIT 1" , nativeQuery = true)
    String getRootRole(String id,String root);



}
